package com.ruoyi.web.shopping.service.impl;

import com.ruoyi.web.shopping.dto.ProductsDTO;
import com.ruoyi.web.shopping.mapper.ProductsMapper;
import com.ruoyi.web.shopping.entity.Products;
import com.ruoyi.web.shopping.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.stream.Collectors;

@Service
public class ProductsServiceImpl implements ProductsService {

    @Autowired
    private ProductsMapper productsMapper;

    /**
     * 新增商品
     *
     * @param products
     */
    public void save(Products products) {
        productsMapper.insert(products);
    }

    /**
     * 删除商品
     *
     * @param id
     */
    public void deleteById(String id) {
        productsMapper.deleteById(id);
    }

    /**
     * 修改商品
     *
     * @param productsDTO
     */
    public void update(ProductsDTO productsDTO) {
        productsMapper.update(productsDTO);
    }


    /**
     * 查询商品
     *
     * @param id
     * @return
     */
    public ProductsDTO selectById(String id) {
        ProductsDTO productsDTO = productsMapper.selectById(id);
        return productsDTO;
    }

    /**
     * 根据商品名查询
     *
     * @param name
     * @return
     */
    public ArrayList<ProductsDTO> selectByName(String name) {
        ArrayList<ProductsDTO> productsDTOs = productsMapper.selectByName(name);
        for (ProductsDTO productsDTO : productsDTOs) {
            String name1 = productsDTO.getName();
            if (name1.equals("蓝莓")) {
                System.out.println(1);
            } else if (name1.equals("梦之蓝")){
                System.out.println(2);
            }
        }
        return productsDTOs;
    }


}
